package com.envmonitor.interfaces.rest;

import com.envmonitor.application.service.EnvironmentDataApplicationService;
import com.envmonitor.domain.model.EnvironmentData;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/data")
@RequiredArgsConstructor
public class EnvironmentDataController {
    
    private final EnvironmentDataApplicationService dataApplicationService;
    
    @GetMapping
    public ResponseEntity<List<EnvironmentData>> getAllData() {
        return ResponseEntity.ok(dataApplicationService.getAllData());
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<EnvironmentData> getDataById(@PathVariable Long id) {
        return dataApplicationService.getDataById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    @GetMapping("/device/{deviceId}")
    public ResponseEntity<List<EnvironmentData>> getDataByDeviceId(@PathVariable Long deviceId) {
        return ResponseEntity.ok(dataApplicationService.getDataByDeviceId(deviceId));
    }
    
    @PostMapping("/device/{deviceId}")
    public ResponseEntity<EnvironmentData> createData(@Valid @RequestBody EnvironmentData data, 
                                                     @PathVariable Long deviceId) {
        return dataApplicationService.createData(data, deviceId)
                .map(savedData -> new ResponseEntity<>(savedData, HttpStatus.CREATED))
                .orElse(ResponseEntity.notFound().build());
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<EnvironmentData> updateData(@PathVariable Long id, 
                                                     @Valid @RequestBody EnvironmentData data) {
        return dataApplicationService.updateData(id, data)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteData(@PathVariable Long id) {
        dataApplicationService.deleteData(id);
        return ResponseEntity.noContent().build();
    }
    
    @GetMapping("/timerange")
    public ResponseEntity<List<EnvironmentData>> getDataByTimeRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end) {
        return ResponseEntity.ok(dataApplicationService.getDataByTimeRange(start, end));
    }
    
    @GetMapping("/alerts/temperature")
    public ResponseEntity<List<EnvironmentData>> getHighTemperatureAlerts(@RequestParam Double threshold) {
        return ResponseEntity.ok(dataApplicationService.getHighTemperatureAlerts(threshold));
    }
    
    @GetMapping("/alerts/humidity")
    public ResponseEntity<List<EnvironmentData>> getHighHumidityAlerts(@RequestParam Double threshold) {
        return ResponseEntity.ok(dataApplicationService.getHighHumidityAlerts(threshold));
    }
    
    @GetMapping("/alerts/energy")
    public ResponseEntity<List<EnvironmentData>> getHighEnergyConsumptionAlerts(@RequestParam Double threshold) {
        return ResponseEntity.ok(dataApplicationService.getHighEnergyConsumptionAlerts(threshold));
    }
}